package com.springnews.controller;

import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@SuppressWarnings("unchecked")
public class NewsController {
	@Resource
	private RedisTemplate redisTemplate;
	@RequestMapping("/index")
	public String index(){
		return "index";
	}
	@RequestMapping("/detail/{id}")
	public String detail(@PathVariable Integer id, Model model) {
		Map<String ,String >map=redisTemplate.opsForHash().entries("news_"+id);
		String countRead=map.get("read");
		if (countRead==null||countRead.equals("")){
			countRead="1";
		}else {
			countRead=(Integer.parseInt(countRead)+1)+"";
		}
		map.put("read",countRead);
		redisTemplate.opsForHash().putAll("news_"+id,map);
		model.addAllAttributes(map);
		return "news"+id;
	}
	@RequestMapping("/addZan")
	@ResponseBody
	public String addZan(Integer id){
		Map<String,String>map=redisTemplate.opsForHash().entries("news_"+id);
		String countZan=map.get("zan");
		if (countZan==null||countZan.equals("")){
			countZan="1";
		}else {
			countZan=(Integer.parseInt(countZan)+1)+"";
		}
		redisTemplate.opsForHash().put("news_"+id,"zan",countZan);
		return countZan;
	}
}
